<template>
	<div class="back_contain_n mine-account">
		<div class="leftList">
			<li v-for="(item,index) in paramList" :key="index"
				:class="{'active': viewType == item.key}"
				@click="changeView(item)"
			>
				<el-badge :value="item.num" class="item" v-if="item.num > 0">
					<span>{{item.name}}</span>
				</el-badge>
				<el-badge class="item" v-else>
					<span>{{item.name}}</span>
				</el-badge>
			</li>
		</div>
		<div class="mainTable">
			<!-- 头部搜索 -->
			<div class="Search_Top_Part">
				<div class="search_list">
					<div>
						<div class="input_flex" style="margin-left: 44px">
							<el-select clearable  v-model="queryForm.name" placeholder="姓名" style="width: 200px">
								<el-option v-for="(item,index) in userList" :key="index" :label="item.userName"
										   :value="item.id"  />
							</el-select>
						</div>
						<div class="input_flex">
							<el-select clearable v-model="queryForm.itemId" placeholder="品类">
								<el-option v-for="(item,index) in itemList" :key="index" :label="item"
										   :value="item"/>
							</el-select>
						</div>
						<div class="input_flex daterange">
							<el-date-picker v-model="time"
											type="daterange"
											range-separator="-"
											start-placeholder="开始日期"
											end-placeholder="结束日期">
							</el-date-picker>
						</div>
					</div>
				</div>
				<div class="search_bt">
					<span class="zll-search" @click="changeView()">搜索</span>
					<span class="zll-search-reset" @click="searchReset()">重置</span>
				</div>
			</div>
			<div class="Search_Top_Input" v-if="viewType == '0'">
				<div class="addNew">
					<span @click="edit({},'add')" v-if="hasRole('') "><i
						class="el-icon-circle-plus-outline"></i> 申报</span>
				</div>
			</div>
			<!-- table -->
			<sys-table
				ref="systable"
				:isMultipleSelection="false"
				:tableData="tableData"
				:tableLoading="tableLoading"
				:tableHeader="tableHeader"
				:scopeWidth="120"
				:backCount="backCount"
				@getBackData="getList"
			>
				<template slot-scope="scope" slot="operate">
					<el-button @click="edit(scope.row,'see')" type="text" size="small">查看</el-button>
					<el-button @click="deleteRow(scope.row)" type="text" size="small">删除</el-button>
				</template>
			</sys-table>

			<!---- 弹框-------->
			<div class="zll-dialog">
				<popout :title="'不良品扣减 · ' + (type == 'edit'?'编辑':(type =='see'?'查看':'申报'))" :visible.sync="addDialog"
						v-if="addDialog" class="bg_Size">
					<add ref="add" slot="content" :type="type" :editData="editData"></add>
					<template slot="bottom">
						<p class="zll-botton Info" @click="addDialog = false">关 闭</p>
						<p class="zll-botton reject" v-if="type !== 'see'"
						   @click="()=>{this.$refs.add.submitForm('addForm')}">立即申报</p>
					</template>
				</popout>
			</div>
		</div>
	</div>
</template>

<script>
	import Add from './add'
	import {formatDate} from "@/plugins/formatDate";

	export default {
		data() {
			return {

				itemList: ['单支连排', '多支', '伸缩', 'U型'],
				viewType: '0',
				paramList: [
					{name: "待扣减", key: '0', num: 0},
					{name: "已扣减", key: '1', num: 0},
				],

				tableLoading: true, //table刷新
				type: '',
				editData: {},

				queryForm: {
					deduceStatus: '',
					name: '',
					itemId: '',
				},
				backCount: 0,
				tableData: [],
				tableHeader: [],
				addDialog: false,
				time: [],

				userList:[],
			}
		},
		methods: {
			getSelectUsers() {
				let self = this;
				self.userList = [];
				self.$http({
					url: "/jinl/login/getLoginAndRoleSelectList",
					method: "post",
				}).then(resp => {
					if (resp.success) {
						self.userList = resp.result;
					}
				});
			},

			changeView(val) {
				this.viewType = val.key;
				this.getList();
			},

			searchReset() { //重置搜索
				this.queryForm = {
					deduceStatus: '',
					name: '',
					itemId: '',
				};
				this.time = [];
				this.getList();
			},

			edit(val, type) {
				let self = this;
				self.addDialog = true;
				self.type = type;
				self.$nextTick(() => {
					self.editData = Object.assign({}, val);
				});
			},

			deleteRow(rowData) {
				let self = this;
				self.$confirm('是否确认删除？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					self.$http({
						url: '/jinl/reduce/deleteOnDutyReduce',
						method: "post",
						params: {id: rowData.id}
					}).then(resp => {
						if (resp.success) {
							self.getList();
							self.$message.success('删除成功！');
							return
						}
						self.$message.error(resp.message);
					});
				});
			},

			refreshPage(){
				let self = this;
				self.addDialog = false;
				self.getList()
			},


			getList(startIndex, pageRow) {
				let self = this;
				self.tableLoading = true;
				if (this.time && this.time.length == 2) {
					//date类型
					self.queryForm.startTime = formatDate(new Date(self.time[0]), 'yyyy-MM-dd')
					self.queryForm.endTime = formatDate(new Date(self.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
				} else {
					self.queryForm.startTime = ''
					self.queryForm.endTime = ''
				}
				let pageInfo = self.$refs.systable.getPageInfo(startIndex);
				if (pageInfo.pageNum == 0||startIndex == 0) {
					self.$refs.systable.resetPage();
				}
				self.queryForm.startIndex = pageInfo.pageNum;
				self.queryForm.totalPage = pageInfo.pageSize;
				let param = Object.assign({}, self.queryForm);
				param.deduceStatus = self.viewType;
				self.$http({
					url: "/jinl/reduce/getOnDutyReduceList",
					method: "post",
					params:param
				}).then(resp => {
					if (resp.success) {
						self.tableLoading = false;
						self.tableData = resp.result;
						self.tableHeader = [
							{"columnValue": "applyTime", "columnName": "申报日期"},
							{"columnValue": "applyUser", "columnName": "申报人"},
							{"columnValue": "dutyDayTask", "columnName": "生产日期"},
							{"columnValue": "itemRefName", "columnName": "品类"},
							{"columnValue": "planName", "columnName": "任务名称"},
							{"columnValue": "gradeValue", "columnName": "规格"},
							{"columnValue": "userNames", "columnName": "姓名"},
							{"columnValue": "deleteCount", "columnName": "扣减件数"},
							{"columnValue": "deduceStatusCn", "columnName": "状态",refColor:true},
						];
						if (resp.result.length > 0) {
							self.backCount = resp.result[0].count;
						} else {
							self.backCount = 0;
						}
						if(self.viewType == '0'){
							self.paramList[0].num = self.backCount;
						}
					}
				});

			},
		},
		mounted() {
			let self = this;
			self.getSelectUsers();
			self.getList();
			self.$eventBus.$on("updateOndutyReduceList", function () {
				self.addDialog = false;
				self.getList();
			})
		},
		components: {
			Add
		},
	}
</script>

<style lang="scss" scoped>
	@import "@/style/SearchTop.scss";

	.mine-account {
		.search_list {
			display: flex;
			justify-content: space-between;
		}

		.leftList {
			display: flex;
			margin-bottom: 15px;

			li {
				list-style: none;
				width: 100px;
				height: 30px;
				line-height: 28px;
				border-bottom: 1px solid #fff;
				color: #333;
				text-align: center;
				cursor: pointer;
				margin-bottom: 10px;
				transition: all .5s;
				position: relative;
				display: flex;
				justify-content: center;

				&.active {
					color: #ff9935;
					border-color: #ff9935;
					position: relative;
					font-weight: 600;
					font-size: 14px;
				}

				.nums {
					position: absolute;
					right: 5px;
					top: 0;
					background: #f44336;
					color: #fff;
					height: 16px;
					line-height: 16px;
					font-weight: 400;
					border-radius: 8px;
					font-size: 12px;
					padding: 0 5px;
				}
			}
		}
	}
</style>
